/* ******************************************
 *	FileProgress Object
 *	Control object for displaying file info
 * ****************************************** */

function FileProgress(file, customSettings) {
	this.disappearTimeOut=100000;
	this.imageSizeWidth=245;
	this.imageSizeHeight=200;
	this.customSettings=customSettings;
	if(customSettings.type==2){
		this.fileProgressID = file.id;		
	}
	else{
		this.fileProgressID = "divFileProgress";
		
	}
	targetID=customSettings.uploadContainer;
	
	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";
		
		
		
		if(customSettings.type==2){
			this.fileProgressWrapper.style.cssFloat="left";
			this.imageContainer = document.createElement("div");
			this.imageContainer.className = "galleryImageContainer";
			this.imageContainer.style.width=this.imageSizeWidth+"px";
			this.imageContainer.style.height=this.imageSizeHeight+"px";
			this.fileProgressWrapper.appendChild(this.imageContainer);
			if(customSettings.fieldName!=undefined){
				this.hiddenFieldInput = document.createElement("input");
				this.hiddenFieldInput.className = "inputHidden";
				this.hiddenFieldInput.type = "hidden";
				this.hiddenFieldInput.name = customSettings.fieldName+"["+this.fileProgressID+"]";
				this.fileProgressWrapper.appendChild(this.hiddenFieldInput);
			}
		}		
		
		
		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		fadeIn(this.fileProgressWrapper, 0);

	} else {
		//get progressContainer
		this.fileProgressElement = this.fileProgressWrapper.firstChild;	
		if(this.customSettings.type==2){
			while(this.fileProgressElement.getAttribute('class')==undefined || this.fileProgressElement.getAttribute('class').length<17 || this.fileProgressElement.getAttribute('class').substr(0,17)!='progressContainer')
			{	
				if(this.fileProgressElement.getAttribute('class')!=undefined && this.fileProgressElement.getAttribute('class')=="galleryImageContainer")this.imageContainer=this.fileProgressElement;
				else if(this.fileProgressElement.getAttribute('class')!=undefined && this.fileProgressElement.getAttribute('class')=="inputHidden")this.hiddenFieldInput=this.fileProgressElement;
				this.fileProgressElement =this.fileProgressElement.nextSibling;
				//alert(this.fileProgressElement.getAttribute('class'));
			}
			this.fileProgressElement.childNodes[1].firstChild.nodeValue = file.name;
		}
		else{
			if(this.customSettings.fieldName!=undefined)this.hiddenFieldInput=document.getElementsByName(this.customSettings.fieldName).item(0);//$("input[name='"+this.customSettings.fieldName+"']");
		}
	}
	
	this.height = this.fileProgressWrapper.offsetHeight;
	
}

FileProgress.prototype.addImage = function (src) {
	newImg=document.createElement("img");		
	if(this.imageContainer!=undefined){
		this.imageContainer.appendChild(newImg);
		newImg.imageSizeWidth=this.imageSizeWidth;
		newImg.imageSizeHeight=this.imageSizeHeight;
		
		newImg.onload = function () {
			var newSize = new resizeImage(newImg.width,newImg.height,this.imageSizeWidth,this.imageSizeHeight);
			newImg.width=newSize.width;
			newImg.height=newSize.height;
			newImg.style.marginLeft=(this.imageSizeWidth-newSize.width)/2+"px";
			newImg.style.marginTop=(this.imageSizeHeight-newSize.height)/2+"px";
			newImg.style.position="absolute";
		};
	}
	else{
		$('#'+this.customSettings.uploadContainer).find('img').remove();
		$('#'+this.customSettings.uploadContainer).children().first().before(newImg);
		newImg.style.position="relative";
	}	
	
	newImg.src =src;	
	
};
FileProgress.prototype.setHiddenField = function (value) {
	if(this.hiddenFieldInput!=undefined){
		this.hiddenFieldInput.value=value
	}	
};
FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";	
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";	
	
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";
	if(this.customSettings.type==2 && this.customSettings.fieldTitle!=undefined){
		var imageItem=document.createElement("div");
		imageItem.className="imageItem";		
		imageItem.style.width=(this.imageSizeWidth-10)+"px";
		imageItem.style.paddingRight="10px";
		imageItem.style.display="none";
		imageItem.innerHTML='<a href="javascript:;" onclick="albumCover(\''+this.customSettings.filename+'\',\''+this.customSettings.catid+'\')" title="Album Cover">&nbsp;&nbsp;Album Cover&nbsp;&nbsp;</a>';
		imageItem.innerHTML+='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:;" onclick="removeImage(\''+this.fileProgressID+'\',\''+this.customSettings.filename+'\')" title="remove">&nbsp;&nbsp;X&nbsp;&nbsp;</a>';
		
		this.imageContainer.appendChild(imageItem);
		$('#'+this.fileProgressID).find('.galleryImageContainer').mouseenter(function(){			
			var imgItem=$('#'+this.parentNode.id).find('.imageItem');			
			imgItem.fadeIn(100)
		}).mouseleave(function(){
			var imgItem=$('#'+this.parentNode.id).find('.imageItem');
			imgItem.fadeOut(100)
		});		
		this.fileProgressWrapper.removeChild(this.fileProgressElement);
		//t(this.customSettings.fieldTitle.split(","));
		var arrField=this.customSettings.fieldTitle.split(",");

		for(i=0;i<arrField.length;i++){

			var textFieldInput = document.createElement("input");
			textFieldInput.style.marginTop="5px";
			if(i==arrField.length-1)textFieldInput.style.marginBottom="45px";
			textFieldInput.style.width="250px";
			textFieldInput.name=arrField[i]+"["+this.customSettings.filename+"]";
			this.fileProgressWrapper.appendChild(textFieldInput);
		}
	}
	

};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
	if(this.customSettings.type==2 ){
		var imageItem=document.createElement("div");
		imageItem.className="imageItem";		
		imageItem.style.width=(this.imageSizeWidth-10)+"px";
		imageItem.style.paddingRight="10px";
		imageItem.style.display="none";
		imageItem.innerHTML='<a href="javascript:;" onclick="removeImage(\''+this.fileProgressID+'\',\''+this.customSettings.filename+'\')" title="remove">&nbsp;&nbsp;X&nbsp;&nbsp;</a>';
		this.imageContainer.appendChild(imageItem);
		$('#'+this.fileProgressID).find('.galleryImageContainer').mouseenter(function(){			
			var imgItem=$('#'+this.parentNode.id).find('.imageItem');			
			imgItem.fadeIn(100)
		}).mouseleave(function(){
			var imgItem=$('#'+this.parentNode.id).find('.imageItem');
			imgItem.fadeOut(100)
		});
		
	}
	

};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
	

};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfuploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfuploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};
